/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package JFreeChartExtensions.chartsExporters.impl;

import JFreeChartExtensions.ExtendableChartPanel;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfWriter;
import java.awt.Dimension;
import java.awt.Graphics2D;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 *
 * @author Nobody
 */
public class PDFChartExporter implements IChartExporter {
    @Override
    public void export(ExtendableChartPanel extendableChartPanel, File file, Dimension dimension) throws IOException {
        Document itextDoc = new Document(new Rectangle(0, 0, (int) dimension.width, (int) dimension.height));
        PdfWriter writer = null;
        try {
            writer = PdfWriter.getInstance(itextDoc, new FileOutputStream(file));
        } catch (DocumentException ex) {
            throw new IOException(ex);
        }
        itextDoc.open();
        PdfContentByte cb = writer.getDirectContent();
        cb.saveState();
        Dimension dim = extendableChartPanel.getSize();
        extendableChartPanel.setSize(dimension);
        Graphics2D g2 = cb.createGraphics(dimension.width, dimension.height);
        extendableChartPanel.paintAll(g2);
        g2.dispose();
        extendableChartPanel.setSize(dim);
        extendableChartPanel.repaint();
        cb.restoreState();
        itextDoc.close();
    }
}